package com.dq.controller.user;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.AdjustingRequest;
import com.dq.controller.in.StaffQuitRequest;
import com.dq.controller.in.StaffUserChangeQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;

import com.dq.domain.org.Org;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.record.StaffUserChangeRecord;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.service.AllService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/12/26 15:12
 */
@RestController
@RequestMapping("/sys/boss/bossStaffUserChangeRecord")
@Api(description = "boss 员工变更记录")
public class BossStaffUserChangeRecordController extends AllService {


    @ApiOperation("列表")
    @PostMapping("list")
    public Vo list(@RequestBody StaffUserChangeQuery staffUserChangeQuery)throws AdminException{
        List<StaffUserChangeRecord> records = staffUserChangeRecordService.findByWhere(staffUserChangeQuery);
        Long total = staffUserChangeRecordService.countByWhere(staffUserChangeQuery);
        List<StaffUserChangeRecord.StaffUserChangeRecordVo> list = new ArrayList<>();
        records.forEach(record ->{
            list.add(record.toStaffUserChangeRecordVo());
        });
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setTotal(total);
        return new Vo(pageInfo);
    }


    @ApiOperation("离职")
    @PostMapping("staffQuit")
    @Transactional
    public Vo staffQuit(@RequestBody StaffQuitRequest staffQuitRequest) throws AdminException {
        StaffUser staffUserQuit = dao.get(StaffUser.class,staffQuitRequest.staffQuitId);
        if(staffUserQuit == null){
            throw new AdminException("所选技师不能为空");
        }
        staffUserChangeRecordService.staffQuit(staffUserQuit);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("调岗")
    @PostMapping("adjusting")
    public Vo adjusting(@RequestBody AdjustingRequest adjustingRequest) throws AdminException{
        StaffUser staffUserJust = dao.get(StaffUser.class,adjustingRequest.getStaffJustId());
        /*if(wxUserRepository.findByStaffUserAndDeletedFalse(staffUserJust).size()>0){
            throw new AdminException("所选技师下还有绑定顾客，请先转移顾客");
        }*/
        //转移目标技师
        if(adjustingRequest.getStaffUserId() == null){
            throw new AdminException("请选择转移目标技师");
        }
        StaffUser staffUser = dao.get(StaffUser.class,adjustingRequest.getStaffUserId());
        if(staffUser == null){
            throw new AdminException("转移目标技师不能为空");
        }
        if(adjustingRequest.getStaffJustId() == adjustingRequest.getStaffUserId()){
            throw new AdminException("转移技师和目标技师相同，请重新选择");
        }
        OrgPosition orgPosition = dao.get(OrgPosition.class,adjustingRequest.getOrgPositionId());
        if(orgPosition == null){
            throw new AdminException("所选岗位不能为空");
        }
        if(staffUserJust.getOrgPosition() == orgPosition){
            throw new AdminException("岗位相同 请重新选择");
        }
        Org org = null;
        if(adjustingRequest.getType() == StaffUserChangeRecord.StaffUserChangeType.outTransfer){
            org = dao.get(Org.class, adjustingRequest.getNowOrgId());
        }else {
            org = (Org)shiroService.getDept();
        }
        staffUserChangeRecordService.adjusting(orgPosition, org,staffUserJust,adjustingRequest.getType());
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("根据用户姓名模糊查")
    @GetMapping("getLikeInfo/{userRealCName}")
    public Vo getLikeInfo(@PathVariable @ApiParam("姓名")String userRealCName) throws AdminException {
        List<WxUser> list= wxUserRepository.findByUserRealcNameLikeAndDeletedFalse(BaseAdminQuery.likeSql(userRealCName));
        List<WxUser.SimpleWxUser> wxUserList = new ArrayList<>();
        if(list.size() == 0){
            throw new AdminException("无此用户");
        }
        list.forEach(wxUser -> {
            wxUserList.add(wxUser.toSimpleWxUser(wxUser.getSection().getSimpleName()));
        });
        return new Vo(wxUserList);
    }

    @GetMapping("/findStaff")
    @ApiOperation("查询门店下的员工")
    public Vo findStaff() throws AdminException{
        return communalService.findStaffByOrg();
    }
}
